﻿using System;
using System.Collections.Generic;
using System.Text;
using ClearSlideLibrary.Dom;
using System.IO;

namespace ClearSlideLibrary.HtmlController
{
    internal class HtmlSlide : HtmlPresentationElement
    {
        public PPTContainerShape ContainerShape { get; set; }
        private readonly string _fileName;
        private readonly string dirPath;
        private int slideIndex;
        
        public HtmlSlide(string slideid, string slideClass, string fileName, string dirpath, int slideIndex)
        {
            base.id = slideid;
            base.cssClass = slideClass;
            this._fileName = fileName;
            this.dirPath = dirpath;
            this.slideIndex = slideIndex;
        }

        public override string DrawElement()
        {
            StringBuilder slideBuilder = new StringBuilder(1000);
            slideBuilder.Append("<div class=\"" + cssClass + "\" id=\"" + id + "\"><img />");
            try
            {
              
                GenerateElements(slideBuilder);
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("The slide has no shapes." + ex);
            }
            slideBuilder.Append("</div>");

            return slideBuilder.ToString();
        }

        private void GenerateElements(StringBuilder slide)
        {
            if (ContainerShape.Elements.Count > 0)
            {
                foreach (PPTShapeBase baseShape in ContainerShape.Elements)
                {
                    DrawShapeHtml(slide, baseShape);
                }
            }
        }

        private void DrawShapeHtml(StringBuilder slide, PPTShapeBase baseShape)
        {
            int left = baseShape.VisualShapeProp.Left();
            int width = baseShape.VisualShapeProp.PixelWidth();
            int top = baseShape.VisualShapeProp.Top();
            int height = baseShape.VisualShapeProp.PixelHeight();
            string Id = baseShape.NonVisualShapeProp.Id;
            bool invisible = baseShape.Invisible;
            bool animatable = baseShape.Animatable;
            string clickLinkUrl = baseShape.ClickLinkUrl;


            //simple fix (not good but work for now).
            DirectoryInfo dest = new DirectoryInfo(dirPath);
            FileInfo[] files = dest.GetFiles();

            foreach (FileInfo file in files)
            {
                string fname = Path.GetFileNameWithoutExtension(file.Name);

                if (fname.Equals(Id))
                {
                    if (baseShape is PPTGraphicFrame)
                    {
                        HtmlSmartArt htmlSmartArt = new HtmlSmartArt(Id, width, height, top,
                                                                    left, invisible, animatable);
                        slide.Append(htmlSmartArt.DrawElement());
                    }
                    else if (baseShape is PPTGroupShape)
                    {
                        HtmlGroupShape htmlShape = new HtmlGroupShape(Id, width, height, top,
                                                                        left, invisible, animatable);
                        slide.Append(htmlShape.DrawElement());
                    }
                    else if (baseShape is PPTConnectionShape)
                    {
                        HtmlConnectionShape htmlShape =
                            new HtmlConnectionShape(Id, width, height, top,
                                                    left, invisible, animatable);
                        slide.Append(htmlShape.DrawElement());
                    }
                    else if (baseShape is PPTShape)
                    {
                        PPTShape s = (PPTShape)baseShape;

                        HtmlShape htmlShape = new HtmlShape(Id, width, height, top,
                                                             left, invisible, animatable, slideIndex)
                                                 {
                                                     Shape = s,
                                                     Rotate = s.VisualShapeProp.Rotate,
                                                     HyperLink = clickLinkUrl
                                                 };
                        slide.Append(htmlShape.DrawElement());
                    }
                    else if (baseShape is PPTImage)
                    {
                        string imageName = baseShape.NonVisualShapeProp.Name;
              
                        //TODO Differentiate between gif and other image types.

                        //HtmlImageGIF image = new HtmlImageGIF(Id, _fileName, imageName,top, left);
                        //slide.Append(image.DrawElement());

                        HtmlImage htmlImage = new HtmlImage(Id, width, height,
                                                            top, left, invisible, animatable,((PPTImage)baseShape).FileExtension )
                                                            {
                                                                HyperLink = clickLinkUrl
                                                            };
                      
                        slide.Append(htmlImage.DrawElement());
                    }
                }
            }

        }


        public override string ToString()
        {
            Console.WriteLine("The id is:" + id);
            Console.WriteLine("The css class is:" + cssClass);

            return id + cssClass;
        }
    }
}